use std::error::Error;

use crate::ast::{CompUnit, FuncType};

pub fn to_koopa(comp_unit: &CompUnit) -> Result<String, Box<dyn Error>> {
    let func_def = &comp_unit.func_def;
    let ret_value = &comp_unit.func_def.block.stmt.number;
    let ret_type_str = match func_def.func_type {
        FuncType::Int => "i32".to_string(),
        _ => "i32".to_string(),
    };
    let ir = format!(
        "fun @{}(): {} {{\n%entry:\n\tret {}\n}}\n",
        func_def.id, ret_type_str, ret_value
    );

    Ok(ir)
}
